#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
using namespace std;


struct ListNode 
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution
{
public:
    ListNode* sortList(ListNode* head)
    {
        ListNode* cur = head;
        int len = 0;
        while (cur != NULL)
        {
            len++;
            cur = cur->next;
        }
        cur = head;
        int* nums = (int*)malloc(sizeof(int) * len);

        for (int i = 0; i < len; i++)
        {
            nums[i] = cur->val;
            cur = cur->next;
        }
        sort(nums, nums + len);
        cur = head;
        for (int i = 0; i < len; i++)
        {
            cur->val = nums[i];
            cur = cur->next;
        }
        return head;
    }
};